home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_10_11 / 1011099a < prev    next >
Text File  |  1992-08-31  |  19KB  |  554 lines

  1.        /***********************************************
  2.        *
  3.        *       file d:\cips\scale.c
  4.        *
  5.        *       Functions: This file contains
  6.        *          zoom_image_array
  7.        *          shrink_image_array
  8.        *          interpolate_pixel
  9.        *          average_pixel
  10.        *          median_pixel
  11.        *          get_scaling_options
  12.        *          blank_image_array
  13.        *
  14.        *       Purpose:
  15.        *          These functions implement image array
  16.        *          zooming (enlarging) and shrinking.
  17.        *
  18.        *       External Calls:
  19.        *          wtiff.c - does_not_exist
  20.        *                    round_off_image_size
  21.        *                    create_allocate_tiff_file
  22.        *                    write_array_into_tiff_image
  23.        *          tiff.c - read_tiff_header
  24.        *          rtiff.c - read_tiff_image
  25.        *          rstring.c - read_string
  26.        *          numcvrt.c - get_integer
  27.        *          filter.c - median_of
  28.        *
  29.        *       Modifications:
  30.        *          8 April 1992 - created
  31.        *
  32.        *************************************************/
  33.  
  34. #include "d:\cips\cips.h"
  35. #include <malloc.h>
  36.  
  37.      /*******************************************
  38.      *
  39.      *   zoom_image_array(...
  40.      *
  41.      *   This function zooms in on an input
  42.      *   image array.  It zooms by enlarging
  43.      *   an input image array and writing the
  44.      *   resulting image arrays to an output
  45.      *   image file.  It can zoom or enlarge by
  46.      *   a factor of 2 or 4.
  47.      *
  48.      *   You can zoom or enlarge an image array
  49.      *   by using either the replication or
  50.      *   interpolation methods.
  51.      *
  52.      *******************************************/
  53.  
  54.  
  55. zoom_image_array(in_name, out_name, the_image, out_image,
  56.           il, ie, ll, le, scale, method)
  57.    char   in_name[], out_name[], method[];
  58.    int    il, ie, ll, le, scale;
  59.    short  the_image[ROWS][COLS],
  60.           out_image[ROWS][COLS];
  61. {
  62.    int    A, B, a, b, count, factor, i, j, length, width;
  63.    struct tiff_header_struct image_header;
  64.  
  65.            /*******************************************
  66.            *
  67.            *   Check the scale factor.  If it is not
  68.            *   a valid factor (2 or 4), then set
  69.            *   it to 2.
  70.            *
  71.            *******************************************/
  72.  
  73.    factor = scale;
  74.    if(factor != 2 &&
  75.       factor != 4) factor = 2;
  76.  
  77.    if(does_not_exist(out_name)){
  78.       printf("\n\n output file does not exist %s", out_name);
  79.       read_tiff_header(in_name, &image_header);
  80.       image_header.image_length = ROWS*factor;
  81.       image_header.image_width  = COLS*factor;
  82.       create_allocate_tiff_file(out_name, &image_header,
  83.                                 out_image);
  84.    }  /* ends if does_not_exist */
  85.  
  86.            /*******************************************
  87.            *
  88.            *   Let me try to explain the nested loops
  89.            *   shown below.
  90.            *
  91.            *   We will enlarge the_image by the factor.
  92.            *   Therefore, we want to divide the_image
  93.            *   into parts of size ROWS/factor by
  94.            *   COLS/factor.  We will loop through
  95.            *   the_image parts ROWS/factor and COLS/factor
  96.            *   using the loops over i and j.
  97.            *
  98.            *   We divided the_image into parts so we must
  99.            *   include all of these parts.  That is why we
  100.            *   do the loops over A and B.  There are
  101.            *   factor*factor parts.
  102.            *
  103.            *   Finally, we do the loops over a and b.
  104.            *   We must replicate every element in the_image
  105.            *   factor*factor times and put these into
  106.            *   out_image.  The a and b loops perform this
  107.            *   task.
  108.            *
  109.            *   The equations inside the []'s for
  110.            *   the_image and out_image are also confusing.
  111.            *   If you work them out, you'll see that we
  112.            *   are bouncing through the image arrays
  113.            *   and fitting the pixels into the right
  114.            *   places.
  115.            *
  116.            *   The final proof is that this works.
  117.            *
  118.            *   One final note:  the factor should divide
  119.            *   evenly into ROWS.  For example, ROWS=100
  120.            *   so using a factor of 8 is not good.
  121.            *   100/8 = 12.5 and this leave you with
  122.            *   an empty strip along the right and
  123.            *   bottom edges of the out_image.
  124.            *
  125.            *******************************************/
  126.  
  127.            /*******************************************
  128.            *
  129.            *   Replication method
  130.            *
  131.            *******************************************/
  132.  
  133.    if(method[0] == 'r' || method[0] == 'R'){
  134.       read_tiff_image(in_name, the_image, il, ie, ll, le);
  135.       count = 1;
  136.       for(A=0; A<factor; A++){
  137.        for(B=0; B<factor; B++){
  138.         for(i=0; i<ROWS/factor; i++){
  139.          for(j=0; j<COLS/factor; j++){
  140.           for(a=0; a<factor; a++){
  141.            for(b=0; b<factor; b++){
  142.              out_image[factor*i+a][factor*j+b] =                  
  143.               the_image[i+A*ROWS/factor][j+B*COLS/factor];
  144.            }  /* ends loop over b */
  145.           }  /* ends loop over a */
  146.          }  /* ends loop over j */
  147.         }  /* ends loop over i */
  148.         printf("\nzooming replication %3d of %3d",
  149.                count++, factor*factor);
  150.         write_array_into_tiff_image(out_name, out_image,
  151.             1+A*ROWS, 1+B*COLS, 101+A*ROWS, 101+B*COLS);
  152.        }  /* ends loop over B */
  153.       }  /* ends loop over A */
  154.    }  /* ends replication method */
  155.  
  156.            /***************************
  157.            *
  158.            *   Interpolation method
  159.            *
  160.            ****************************/
  161.  
  162.    if(method[0] == 'i' || method[0] == 'I'){
  163.       read_tiff_image(in_name, the_image, 
  164.                       il, ie, ll, le);
  165.       count = 1;
  166.       for(A=0; A<factor; A++){
  167.          for(B=0; B<factor; B++){
  168.           for(i=0; i<ROWS/factor; i++){
  169.            for(j=0; j<COLS/factor; j++){
  170.             for(a=0; a<factor; a++){
  171.              for(b=0; b<factor; b++){
  172.                 out_image[factor*i+a][factor*j+b] =
  173.                   interpolate_pixel(the_image, A, B,
  174.                               i, j, a, b, factor);
  175.              }  /* ends loop over b */
  176.             }  /* ends loop over a */
  177.            }  /* ends loop over j */
  178.           }  /* ends loop over i */
  179.        printf("\nzooming interpolation %3d of %3d",
  180.                      count++, factor*factor);
  181.        write_array_into_tiff_image(out_name, out_image,
  182.                  1+A*ROWS, 1+B*COLS, 
  183.                  101+A*ROWS, 101+B*COLS);
  184.      }  /* ends loop over B */
  185.     }  /* ends loop over A */
  186.    }  /* ends interpolation method */
  187.  
  188.  
  189. }  /* ends zoom_image_array */
  190.  
  191.  
  192.  
  193.  
  194.     /***********************************************
  195.     *
  196.     *    interpolate_pixel(...
  197.     *
  198.     *    This function interpolates between pixel
  199.     *    values and returns the interlopated value.
  200.     *
  201.     ***********************************************/
  202.  
  203. interpolate_pixel(the_image, A, B, i, j, a, b, factor)
  204.    int   A, B, a, b, factor, i, j;
  205.    short the_image[ROWS][COLS];
  206. {
  207.    int   num, x = 0, y = 0;
  208.    short diff, result;
  209.  
  210.    if(a > 0) y = 1;
  211.    if(b > 0) x = 1;
  212.    diff = the_image[y+i+A*ROWS/factor][x+j+B*COLS/factor] -
  213.           the_image[i+A*ROWS/factor][j+B*COLS/factor];
  214.  
  215.           /***********************************************
  216.           *
  217.           *    If you are at the edge of the input image
  218.           *    array, then you cannot interpolate to the
  219.           *    next point because there is no next point.
  220.           *    Therefore, set the diff to 0.
  221.           *
  222.           ***********************************************/
  223.  
  224.    if((y+i+A*ROWS/factor) >= ROWS) diff = 0;
  225.    if((x+j+B*COLS/factor) >= COLS) diff = 0;
  226.  
  227.    num = a+b;
  228.    if(num > factor) num = factor;
  229.    result = the_image[i+A*ROWS/factor][j+B*COLS/factor] +
  230.             num*diff/factor;
  231.    return(res